package com.self.smallma.myblogweb.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.smallma.myblogweb.common.utils.PageUtils;
import com.self.smallma.myblogweb.common.utils.Query;

import com.self.smallma.myblogweb.dao.MessageDao;
import com.self.smallma.myblogweb.entity.MessageEntity;
import com.self.smallma.myblogweb.service.MessageService;


@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageDao, MessageEntity> implements MessageService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MessageEntity> page = this.page(
                new Query<MessageEntity>().getPage(params),
                new QueryWrapper<MessageEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveHasTime(MessageEntity message) {
        message.setCreateTime(new Date());
        this.save(message);
    }
    //存放迭代找出的所有子代的集合
    private List<MessageEntity> tempReplys = new ArrayList<>();
    @Override
    public List<MessageEntity> listMessage() {
        List<MessageEntity> messageEntities = this.baseMapper.selectList(new QueryWrapper<MessageEntity>()
                .eq("parent_message_id", "-1").orderByDesc("create_time"));

        for (MessageEntity item :messageEntities){
            Long id = item.getId();
            String nickname = item.getNickname();
            List<MessageEntity> childMessages  = this.baseMapper.selectList(new QueryWrapper<MessageEntity>()
                    .eq("parent_message_id", id).orderByDesc("create_time"));
            //查询出子留言
            combineChildren(childMessages,nickname);
            item.setReplyMessages(tempReplys);
            tempReplys = new ArrayList<>();
        }
        return messageEntities;
    }

    private void combineChildren(List<MessageEntity> childMessages, String parentNickname){
        //判断是否有一级子回复
        if (childMessages.size()>0){
            //循环找出子留言的id
            for (MessageEntity childMessage : childMessages){
                String pNickname = childMessage.getNickname();
                childMessage.setParentNickname(parentNickname);
                tempReplys.add(childMessage);
                Long childId = childMessage.getId();
                //查询二级以及所有子集回复
                recursively(childId, pNickname);
            }
        }

    }

    /**
     * 循环迭代找出子集回复
     * @param childId
     * @param pNickname
     */
    private void recursively(Long childId, String pNickname) {
        List<MessageEntity> childMessages  = this.baseMapper.selectList(new QueryWrapper<MessageEntity>()
                .eq("parent_message_id", childId).orderByDesc("create_time"));
        if (childMessages.size()>0){
            for (MessageEntity replayMessage :childMessages){
                String nickname = replayMessage.getNickname();
                replayMessage.setParentNickname(pNickname);
                Long replayId = replayMessage.getId();
                tempReplys.add(replayMessage);
                //递归找出子集回复
                recursively(replayId,nickname);
            }
        }

    }

}